﻿/***************************************************************************
//* Foxit Software Inc.
//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
//* and trade secrets of Foxit Software Inc.                                                      *
***************************************************************************/

/**
* PDF页面数据
*/
define(function (require, exports, module) {
    require('../DataLevel.js');
    var CPDF_AnnotList = require('./AnnotList.js');

    FXREADER.PDFData.PDFDataJsonFormat = {
        PageWidth: "width",
        PageHeight: "height",
        PageRotate: "rotate",
        PageIndex: "number"
        //Annots: "annots"
    };

	/**
    * PDF Page
    * @class PDF Page
    * @constructor
    * @param {String} pageJsonData 	 page information in JSON format.
    * @return a PDF page object.
    */
    FXREADER.PDFData.CPDF_Page = function (pageJsonData) {
        var _nPageIndex,                //页面索引
             _nPageWidth,               //页面宽度
             _nPageHeight,              //页面高度
             _nRotate = 0,               //页面旋转
             _AnnotList = null;            //注释
        var _self = this;
        var PDFDataFmt = FXREADER.PDFData.PDFDataJsonFormat;
        if (!_Init())
            throw new Error("Init PDFPage Failed:" + pageJsonData);

        function _Init() {
            try {
                _nPageIndex = pageJsonData[PDFDataFmt.PageIndex];
                _nPageWidth = pageJsonData[PDFDataFmt.PageWidth];
                _nPageHeight = pageJsonData[PDFDataFmt.PageHeight];
                _nRotate = pageJsonData[PDFDataFmt.PageRotate];
                _AnnotList = new CPDF_AnnotList(_nPageIndex);
            }
            catch (ex) {
                throw ex;
            }
            return true;
        }

        function _GetAnnotInList(szAnnotName, AnnotList) {
            if (AnnotList == null)
                return null;
            var iAnnotCnt = AnnotList.length;
            for (var i = 0; i < iAnnotCnt; i++) {
                var pAnnot = AnnotList[i];
                if (pAnnot) {
                    if (pAnnot.GetAnnotName() === szAnnotName)
                        return pAnnot;
                    if (pAnnot.GetType() == "Markup") {
                        var pFindAnnot = _GetAnnotInList(szAnnotName, pAnnot.GetGroupAnnotList());
                        if (pFindAnnot)
                            return pFindAnnot;
                    }

                }

            }
            return null;
        }


        /**
        * @description Get the width of PDF page. 
        * @return {Number} the width of PDF page. 
        */
        this.GetPageWidth = function () {
            return _nPageWidth;
        };
		
        /**
        * @description Get the height of PDF page. 
        * @return {Number} the height of PDF page. 
        */
        this.GetPageHeight = function () {
            return _nPageHeight;
        };

        this.SetPageWidth = function (width) {
            _nPageWidth = width;
        };

        this.SetPageHeight = function (height) {
            _nPageHeight = height;
        };

        /**
        * @description Get the page index of the page. 
        * @return {Number} the page index of the page. 
        */
        this.GetPageIndex = function () {
            return _nPageIndex;
        };

        this.GetPageRotate = function () {
            return _nRotate;
        };
        this.SetPageRotate = function (nRotate) {
            _nRotate = nRotate;
        };

        //获取Annot
        this.GetAnnot = function (nAnnotIndex) {
            return _AnnotList.GetAt(nAnnotIndex);
        };


        this.GetAnnotByName = function (szAnnotName) {
            if (_AnnotList == null)
                return null;
            return _GetAnnotInList(szAnnotName, _AnnotList.GetAnnotList());
        };


        this.AddAnnot = function (annot) {
            return _AnnotList.Add(annot);
        };
        
        this.GetAnnotsCount = function()
        {
        	return _AnnotList.Count();
        };

        this.DeleteAnnot = function (nAnnotIndex) {
            return _AnnotList.Remove(nAnnotIndex);
        };

        this.GetAnnotIndex = function (pAnnot) {
            if (pAnnot == null)
                return -1;
            var iAnnotCnt = _self.GetAnnotCount();
            for (var i = 0; i < iAnnotCnt; i++) {
                if (pAnnot === _self.GetAnnot(i))
                    return i;
            }
            return -1;
        };

        this.GetAnnotCount = function () {
            return _AnnotList.Count();
        };

    };

    return FXREADER.PDFData.CPDF_Page;
});